var path = require('path');
var express = require('express');
var webpack = require('webpack');
var config = require('./webpack.config');

var app = express();
var compiler = webpack(config);

app.use(require('webpack-dev-middleware')(compiler, {
  noInfo: true,
  publicPath: config.output.publicPath
}));

app.use(require('webpack-hot-middleware')(compiler));

app.get('*', function(req, res) {
  console.log(req.originalUrl.indexOf('js/'));
  //console.log(req.originalUrl);

  var filePath;
  console.log(req.originalUrl);
  if(req.originalUrl.indexOf('index.html') > 0) {
    filePath = path.join(__dirname, 'index.html');
  }
  else {
    filePath = path.join(__dirname, req.originalUrl);
  }

  res.sendFile(filePath);
});

app.listen(3000, 'localhost', function(err) {
  if (err) {
    console.log(err);
    return;
  }

  console.log('Listening at http://localhost:3000');
});